package com.techventus.locations;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class NewLocation extends Activity{

	
	 Button saveButton;
	 Button cancelButton;
	 EditText locationBox;
	 EditText metresBox;
	 
	@Override protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); 
        setContentView(R.layout.namenewlocation);

       // LinearLayout layout = (LinearLayout) findViewById(R.id.namenewlocation);
        Log.e("TECHVENTUSPHONE","NEWLOCATION MENU - List Phone etc...");
        SQLiteDatabase sql =  openOrCreateDatabase("db",0,null);
        SQLHelper.listPhoneAndLocationPhoneEnable(sql);
        sql.close();
        
         saveButton = (Button)findViewById(R.id.NewLocationSaveButton);
        saveButton.setOnClickListener(saveButtonListener);
        
    	//ClickListener saveHandle = new ClickListener(){
        
        cancelButton= (Button)findViewById(R.id.NewLocationCancelButton);
        cancelButton.setOnClickListener(cancelButtonListener);
        
        
        locationBox = (EditText)findViewById(R.id.namenewlocationedittext);
        metresBox = (EditText)findViewById(R.id.newlocationmetresbox);
        
        
    	};
	
    OnClickListener cancelButtonListener = new OnClickListener(){

    		@Override
    		public void onClick(View arg0) {
    			try {
    				
					NewLocation.this.finish();
					
				} catch (Throwable e) {
					e.printStackTrace();
				}
    			
    		}
    	};

	OnClickListener saveButtonListener = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			if(!(locationBox.getText().toString()).equals("")){
				
				Intent i =new Intent(NewLocation.this,LocationMap.class);
				
				i.putExtra("isNew", true);
				
				if((metresBox.getText().toString()).equals("")){
					//Bundle bundle = new Bundle();
					i.putExtra("locationName", locationBox.getText().toString().replace("'","" ));
					i.putExtra("radius", 100);
					
					System.out.println(i.getStringExtra("locationName"));
					//i.putExtra("location", bundle);
				}else{
					//Bundle bundle = new Bundle();
					i.putExtra("locationName", locationBox.getText().toString().replace("'","" ));
					
					i.putExtra("radius", Integer.valueOf(metresBox.getText().toString()));
					//i.putExtra("location", bundle);
					System.out.println(i.getStringExtra("locationName"));
				}
				startActivity(i);
				NewLocation.this.finish();
			}else{
				Toast.makeText(NewLocation.this,"Location Cannot Be Null",Toast.LENGTH_SHORT);
			}
		}
	};
	
}
